package cn.ejiajunxy.manager;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.leo.baseui.ui.BaseActivity;
import com.leo.utils.CheckUtils;

import cn.ejiajunxy.manager.api.ApiService;
import cn.ejiajunxy.manager.model.TokenModel;
import cn.ejiajunxy.manager.utils.ToastUtils;
import cn.ejiajunxy.manager.utils.UserPrefManager;

/**
 * Created by zhangguobao on 2017/8/31.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    public static final int sTYPE_TOKEN = 1;
    private EditText mEtAccount;
    private EditText mEtPwd;
    private TextView mTvForget;
    private TextView mTvRegister;
    private TextView mTvVertification;
    private Button mBtLogin;
    private Dialog loading_dialog;
    private int mType;
    private static final String KEY_TYPE = "type";

    @Override
    protected void init() {
        getParams();
        getToolbar().setMiddleTitle("登录");
        getToolbar().setNavigationOnClickListener(null);
        getToolbar().setNavigationIcon(null);
        initViews();
    }

    private void getParams() {
        mType = getIntent().getIntExtra(KEY_TYPE, 0);
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(starter);
    }

    public static void start(Context context, int type) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.putExtra(KEY_TYPE, type);
        starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(starter);
    }

    private void initViews() {
        loading_dialog = new AlertDialog.Builder(this).setMessage("正在加载...").create();
        mEtAccount = findViewById(R.id.etPhone);
        mEtPwd = findViewById(R.id.etPwd);
        mBtLogin = findViewById(R.id.btLogin);
        mTvForget = findViewById(R.id.tvForget);
        mTvRegister = findViewById(R.id.tvRegister);
        mBtLogin = findViewById(R.id.btLogin);
        mTvVertification = findViewById(R.id.tvVerification);
        mEtAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (check()) {
                    mBtLogin.setEnabled(true);
                }
            }
        });

        mEtPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (check()) {
                    mBtLogin.setEnabled(true);
                }
            }
        });

        mTvForget.setOnClickListener(this);
        mTvRegister.setOnClickListener(this);
        mBtLogin.setOnClickListener(this);
        mTvVertification.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvForget:
                break;
            case R.id.tvRegister:
                break;
            case R.id.btLogin:
                login();
                break;
            case R.id.tvVerification:
                break;
        }
    }

    private void login() {
        final String account = mEtAccount.getText().toString().trim();
        String password = mEtPwd.getText().toString().trim();
        //"18624423286", "Sa123456"
        RxHttpUtils.createApi(ApiService.class).getToken("password", account, password)
                .compose(Transformer.<TokenModel>switchSchedulers(loading_dialog)).subscribe(new CommonObserver<TokenModel>() {
            @Override
            protected void onError(String s) {
                loading_dialog.dismiss();
                ToastUtils.showToast(LoginActivity.this, s);
            }

            @Override
            protected void onSuccess(TokenModel tokenModel) {
                loading_dialog.dismiss();
                if (tokenModel.Success) {
                    UserPrefManager.saveAccount(LoginActivity.this, account);
                    UserPrefManager.saveToken(LoginActivity.this, tokenModel);
                    MainActivity.start(LoginActivity.this);
                    finish();
                } else {
                    ToastUtils.showToast(LoginActivity.this, tokenModel.error_msg);
                }
            }
        });
    }


    private boolean check() {
        if (CheckUtils.isEmpty(mEtAccount.getText().toString())) {
            return false;
        } else if (CheckUtils.isEmpty(mEtAccount.getText().toString())) {
            return false;
        }
        return true;
    }
}
